package com.secneo.appshare.adapter;

import java.io.ByteArrayOutputStream;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.secneo.appshare.R;
import com.secneo.appshare.activities.ApplicationInfoActivity;
import com.secneo.appshare.api.LocalStore;
import com.secneo.appshare.api.RemoteApi.UserActivity;

public class ActivityUserAdapter extends BaseAdapter {
	private List<UserActivity> mList;
	private Context mContext;
	private String mUserName;
	
	public ActivityUserAdapter(Context ctx, List<UserActivity> list, String userName) 
	{
		this.mContext = ctx;
		this.mList = list;
		this.mUserName = userName;
	}
	
	public void freeDrawable()
	{
		for(int i=0; i<mList.size(); i++)
		{
			BitmapDrawable a = (BitmapDrawable)mList.get(i).app_icon_drawable ;
			if(a != null && !a.getBitmap().isRecycled()){
				a.getBitmap().recycle() ;
				mList.get(i).app_icon_drawable = null ;
			}			
		}
		System.gc();
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		ActivityUserItem activityItem;
		final UserActivity activity = mList.get(position);
		if (convertView == null) 
		{
			View v = LayoutInflater.from(mContext).inflate(R.layout.appshare_activity_user_item, null);
			
			activityItem = new ActivityUserItem();
			activityItem.mActivityDetails = (TextView) v.findViewById(R.id.activity_user_details);
			activityItem.mActivityDate = (TextView) v.findViewById(R.id.activity_user_date);
			activityItem.mAppIcon = (ImageView) v.findViewById(R.id.activity_user_app_icon);
			v.setTag(activityItem);
			convertView = v;
		} 
		else 
		{
			activityItem = (ActivityUserItem)convertView.getTag();
		}
		
		if (activity.app_icon_drawable == null)
			activityItem.mAppIcon.setImageResource(R.drawable.some_app);
		else 
			activityItem.mAppIcon.setImageDrawable(activity.app_icon_drawable);
		
		if(activity.action_type == 0)
		{
			activityItem.mActivityDetails.setText(mContext.getString(R.string.activity_action0));
			activityItem.mAppIcon.setImageResource(R.drawable.newbie);
		}
		else if(activity.action_type == 1)
			activityItem.mActivityDetails.setText(mUserName+mContext.getString(R.string.activity_action1)+activity.app_name);
		else if(activity.action_type == 2)
			activityItem.mActivityDetails.setText(mUserName+mContext.getString(R.string.activity_action2)+activity.app_name);
		else if(activity.action_type == 3)
			activityItem.mActivityDetails.setText(mUserName+mContext.getString(R.string.activity_action3)+activity.app_name+":"+activity.comments);
		else if(activity.action_type == 4)
			activityItem.mActivityDetails.setText(mUserName+mContext.getString(R.string.activity_action4)+activity.app_name);
		activityItem.mActivityDate.setText(activity.date);
		
		if(activity.action_type != 0)
			activityItem.mAppIcon.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					try  {
						// Start application activity
						 Intent intent = new Intent(); 
						 intent.setClass(mContext, ApplicationInfoActivity.class);
	
						 Bundle mBundle = new Bundle(); 
						 mBundle.putString("package_name", activity.app_pckName);
						 mBundle.putInt("n", 20);
						 String uid = LocalStore.getLocaleStoreInstance(mContext).getUid();
						 mBundle.putString("uid", uid);
						 mBundle.putBoolean("installed_not_addicted", true);
						 
						PackageManager manager = mContext.getPackageManager();
						try 
						{
							PackageInfo info = manager.getPackageInfo(activity.app_pckName, 0);
							if (info != null)
								mBundle.putBoolean("user_app_flag", true);
							else
								mBundle.putBoolean("user_app_flag", false);
						
						} catch (Exception e) {
							mBundle.putBoolean("user_app_flag", false);
						}
						 
						 /* Send the icon */
						 mBundle.putString("app_icon", activity.app_icon);
						if (activity.app_icon_drawable != null) 
						{
							try 
							{
								Bitmap bitmap = ((BitmapDrawable) activity.app_icon_drawable).getBitmap();
								ByteArrayOutputStream stream = new ByteArrayOutputStream();
	
								bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
								byte[] bitmapdata = stream.toByteArray();
								mBundle.putByteArray("app_icon_array", bitmapdata);
								
							} catch (Exception ex) {
								ex.printStackTrace();
								mBundle.putByteArray("app_icon_array", null);
							}
	
						} else {
							mBundle.putByteArray("app_icon_array", null);
						}
						mBundle.putString("app_name", activity.app_name);
						intent.putExtras(mBundle);
						mContext.startActivity(intent);
						
					} catch(Exception ex) {
						ex.printStackTrace();
					}
				}
			});
		else
			activityItem.mAppIcon.setOnClickListener(null);
		
		return convertView;
	}

	class ActivityUserItem 
	{
		TextView mActivityDetails;
		TextView mActivityDate;
		ImageView mAppIcon;
	}
}
